package shuashuami.hb.com.avtivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.scrat.app.selectorlibrary.ImageSelector;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.ComplaintAdatper;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.SubmitCTaskHttp;

public class ChongxinTijiaoActivity extends AbActivity {

    private GridView gridView;
    private ComplaintAdatper complaintAdatper;
    private List<String> imgs;
    private static final int REQUEST_CODE_SELECT_IMG = 1;
    private static final int MAX_SELECT_COUNT = 9;


    private String id;

    private EditText edtShouhuoXingming, edtShouji, edtDingdanhao;

    private Button btnSubmit;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_chongxin_tijiao);
        gridView = (GridView) findViewById(R.id.gv_complaint);
        edtShouhuoXingming = (EditText) findViewById(R.id.edt_cuntreated_task_info_shouhuoren_xingming);
        edtShouji = (EditText) findViewById(R.id.edt_cuntreated_task_info_shouji_haoma);
        edtDingdanhao = (EditText) findViewById(R.id.edt_cuntreated_task_info_tianxie_dingdan_bianhao);

        btnSubmit = (Button) findViewById(R.id.btn_cuntreated_task_info_submit);
    }

    @Override
    protected void initData() {
        setTitleBar("提交订单");
        setLeftView();
        initAdapter();
        Bundle bundle = this.getIntent().getExtras();/*获取Bundle中的数据，注意类型和key*/
        id = bundle.getString("id");
        edtDingdanhao.setText(bundle.getString("num"));
    }

    private void initAdapter() {
        imgs = new ArrayList<>();
        imgs.add(R.mipmap.ssm_icon_127 + "");
        complaintAdatper = new ComplaintAdatper(this, imgs);
        gridView.setAdapter(complaintAdatper);

    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == imgs.size() - 1) {
                    if (imgs.size() <= 9) {
                        ImageSelector.show(ChongxinTijiaoActivity.this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
                    }
                }
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void showContent(Intent data) {
        List<String> paths = ImageSelector.getImagePaths(data);
        if (!paths.isEmpty()) {
            for (int i = 0; i < paths.size(); i++) {
                imgs.add(0, paths.get(i));
            }
            complaintAdatper.notifyDataSetChanged();
            return;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SELECT_IMG) {
            showContent(data);
            return;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    //提交订单
    private void submit() {
        SubmitCTaskHttp submitCTaskHttp = new SubmitCTaskHttp(this);
        submitCTaskHttp.uploadImg(id, edtShouhuoXingming.getText().toString(), edtShouji.getText().toString(),
                edtDingdanhao.getText().toString(), imgs);
    }
}
